package com.lyhengtongwl.zqsnews.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.adapter.EarnAdapter;
import com.lyhengtongwl.zqsnews.base.BaseActivity;
import com.lyhengtongwl.zqsnews.entity.EarnEntity;
import com.lyhengtongwl.zqsnews.entity.NewsBaseEntity;
import com.lyhengtongwl.zqsnews.net.MyCallback;
import com.lyhengtongwl.zqsnews.net.Task;
import com.lyhengtongwl.zqsnews.utils.SPUtils;
import com.lyhengtongwl.zqsnews.utils.TitleBuilder;
import com.lyhengtongwl.putorefresh.BaseBothListener;
import com.lyhengtongwl.putorefresh.PullToBothLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Response;

public class EarnActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.pullRefresh)
    PullToBothLayout pullToBothLayout;
    private List<EarnEntity> list = new ArrayList<>();
    private int page = 1;
    private boolean isRefresh = true;//默认下拉刷新
    private EarnAdapter newsAdapter = null;

    @Override
    public void setListen() {
        pullToBothLayout.setRefreshListener(new BaseBothListener() {
            @Override
            public void refresh() {
                isRefresh = true;
                page = 1;
                getInComeRecord();
            }

            @Override
            public void loadMore() {
                isRefresh = false;
                page++;
                getInComeRecord();
            }
        });
    }

    @Override
    public void setTitleBarColor() {
        new TitleBuilder(this).setTitleText("收入明细")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_earn;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        LoadData();
    }

    //加载数据
    private void LoadData() {
        list.clear();
        LinearLayoutManager manager = new LinearLayoutManager(this);
        if (newsAdapter == null) {
            newsAdapter = new EarnAdapter(this, list);
        }
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(newsAdapter);
        getInComeRecord();
    }

    //获取收益明细
    private void getInComeRecord() {
        Integer userId = Integer.parseInt(SPUtils.get(App.getContext(), "userId", "") + "");
        Task.getApiService().getInComeRecord(page, userId).enqueue(new MyCallback<NewsBaseEntity>(EarnActivity.this) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                pullToBothLayout.finishLoadMore();
                newsAdapter.notifyDataSetChanged();
                String jsonStr = JSONObject.toJSONString(response.body().getData());
                if (("1").equals(response.body().getCode())) {
//                    JSONObject jsonObject = JSONObject.parseObject(jsonStr);
//                    JSONArray jsonArray = jsonObject.getJSONArray("records");
                    JSONArray jsonArray = JSONArray.parseArray(jsonStr);
                    if (jsonArray.size() > 0) {
                        if (isRefresh) {
                            //表示下拉刷新
                            list.clear();
                            for (int i = 0; i < jsonArray.size(); i++) {
                                EarnEntity earnEntity = new EarnEntity();
                                earnEntity.setMoney(jsonArray.getJSONObject(i).getInteger("moneyNum") + "");
                                earnEntity.setTitle(jsonArray.getJSONObject(i).getString("remark"));
                                earnEntity.setTime(jsonArray.getJSONObject(i).getString("time"));
                                earnEntity.setInComeTypeId(jsonArray.getJSONObject(i).getInteger("incomeTypeId"));
                                earnEntity.setTitle_1(jsonArray.getJSONObject(i).getString("title") + "");
                                earnEntity.setTag(jsonArray.getJSONObject(i).getString("tag") + "");
                                list.add(earnEntity);
                            }
                            pullToBothLayout.finishRefresh();
                            newsAdapter.notifyDataSetChanged();
                        } else {
                            //上拉加载
                            for (int i = 0; i < jsonArray.size(); i++) {
                                EarnEntity earnEntity = new EarnEntity();
                                earnEntity.setMoney(jsonArray.getJSONObject(i).getInteger("moneyNum") + "");
                                earnEntity.setTitle(jsonArray.getJSONObject(i).getString("remark"));
                                earnEntity.setTime(jsonArray.getJSONObject(i).getString("time"));
                                earnEntity.setInComeTypeId(jsonArray.getJSONObject(i).getInteger("incomeTypeId"));
                                earnEntity.setTitle_1(jsonArray.getJSONObject(i).getString("title") + "");
                                earnEntity.setTag(jsonArray.getJSONObject(i).getString("tag") + "");
                                list.add(earnEntity);
                            }
                            pullToBothLayout.finishLoadMore();
                            newsAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }

            @Override
            protected void onFailure(Throwable t) {
                pullToBothLayout.finishLoadMore();
                newsAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }
}
